package com.ruoyi.activiti.domain.dto;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.domain.entity.SysUser;

public class HistoryProcessInstanceDTO {
    private static final long serialVersionUID = 1L;

    private String instanceId;
    private String startUserId;
    private String processDefinitionKey;
    private String taskName;
    
    private Map<String, Object> paramMap;
    
    

	public Map<String, Object> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, Object> paramMap) {
		if(!paramMap.isEmpty()){
			this.paramMap = new HashMap<String, Object>();
			for(String key : paramMap.keySet()){
				if(key.startsWith("q_")){
					this.paramMap.put(key, paramMap.get(key));
				}
			}
		}
	}

	public HistoryProcessInstanceDTO(HistoricActivityInstance task, HistoricProcessInstance inst, Map<String, Object> variables, SysUser user) {
        this.setParamMap(variables);
		extracted(task.getId(), task.getActivityName(), task.getProcessInstanceId(), inst, variables, user);
    }

    public String getStartDeptName() {
        return startDeptName;
    }

    public void setStartDeptName(String startDeptName) {
        this.startDeptName = startDeptName;
    }

    private String startDeptName;
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     *
     * @param task
     * @param inst
     * @param variables
     */
    public HistoryProcessInstanceDTO(org.activiti.engine.task.Task task, HistoricProcessInstance inst, Map<String, Object> variables, SysUser user) {
    	this.setParamMap(variables);
    	extracted(task.getId(), task.getName(), task.getProcessInstanceId(), inst, variables, user);
    }

    public HistoryProcessInstanceDTO(HistoricActivityInstance task, HistoricProcessInstance inst) {
        if (task == null)
            extractedEx("task.getId()", "task.getActivityName()", "task.getProcessInstanceId()", inst);
        else
            extractedEx(task.getId(), task.getActivityName(), task.getProcessInstanceId(), inst);
    }


    private void extractedEx(String taskId, String taskName, String instanceId, HistoricProcessInstance inst) {
        this.setId(taskId);

        this.instanceName = inst.getName();
        this.definitionKey= inst.getProcessDefinitionKey();
        this.processDefinitionName= inst.getProcessDefinitionName();
        this.businessKey= inst.getBusinessKey();
        this.taskName = taskName;
        if ( inst.getStartUserId() != null &&  !inst.getStartUserId().isEmpty()) {
            this.startUserId = inst.getStartUserId();
            this.startDeptName =  "xxx";
        } else {
            this.startUserId = "-";
            this.startDeptName = "-";
        }
        this.setStatus(inst.getEndTime() != null?"完成":"进行中");

        this.instanceId = instanceId;
        this.createdDate = inst.getStartTime();
    }

    private void extracted(String taskId, String taskName, String instanceId, HistoricProcessInstance inst, Map<String, Object> variables, SysUser user) {
        this.setId(taskId);

        this.instanceName = inst.getName();
        this.definitionKey= inst.getProcessDefinitionKey();
        this.processDefinitionName= inst.getProcessDefinitionName();
        this.businessKey= inst.getBusinessKey();
        this.taskName = taskName;
        String startUserName = (String) variables.get("startUserName"); // 暂时闲置
        String startDeptName = (String) variables.get("startDeptName");
        if ( inst.getStartUserId() != null &&  !inst.getStartUserId().isEmpty()) {
            this.startUserId = inst.getStartUserId();
            this.startDeptName = startDeptName + "xxx";
        } else {
            this.startUserId = "-";
            this.startDeptName = "-";
        }
        this.setStatus(inst.getEndTime() != null?"完成":"进行中");

        this.instanceId = instanceId;
        this.createdDate = inst.getStartTime();
    }

    public HistoryProcessInstanceDTO() {

    }

    public String getProcessDefinitionKey() {
        return processDefinitionKey;
    }

    public void setProcessDefinitionKey(String processDefinitionKey) {
        this.processDefinitionKey = processDefinitionKey;
    }

    public String getProcessDefinitionName() {
        return processDefinitionName;
    }

    public void setProcessDefinitionName(String processDefinitionName) {
        this.processDefinitionName = processDefinitionName;
    }

    private String processDefinitionName;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    private String status;

    public String getStartUserId() {
        return startUserId;
    }

    public void setStartUserId(String startUserId) {
        this.startUserId = startUserId;
    }

    public String getInstanceId() {
        return instanceId;
    }

    public void setInstanceId(String instanceId) {
        this.instanceId = instanceId;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String getInstanceName() {
        return instanceName;
    }

    public void setInstanceName(String instanceName) {
        this.instanceName = instanceName;
    }

    public String getDefinitionKey() {
        return definitionKey;
    }

    public void setDefinitionKey(String definitionKey) {
        this.definitionKey = definitionKey;
    }

    public String getBusinessKey() {
        return businessKey;
    }

    public void setBusinessKey(String businessKey) {
        this.businessKey = businessKey;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createdDate;
    private String instanceName;
    private String definitionKey;
    private String businessKey;
}
